package com.xbb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbb.entity.Message;
import com.xbb.service.IMessageService;
import com.xbb.util.Result;
import com.xbb.util.Result1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr xie
 * @since 2020-06-15
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    IMessageService iMessageService;

    @RequestMapping("/getMessage")
    public Object getMessage(Integer id) {
        Message message = iMessageService.getMessage(id);
        if(message!=null) {
            return new Result1(0,"获取成功",message);
        }
        return new Result1(1,"获取失败");
    }

    @RequestMapping("/getMessages")
    public Object getMessages(Integer id,Integer page,Integer limit) {
        List<Message> messages = iMessageService.getMessages(id);
        if(messages.size()>0) {
            return new Result1(0,"获取成功",messages.size(),messages);
        }
        return new Result1(1,"获取失败");
    }


    @RequestMapping("/addMessage")
    public Object addMessage(Message message) {
        System.out.println(message);
        message.setIsOpen("未打开");
        message.setCreateTime(new Date());
        boolean save = iMessageService.save(message);
        if(save) {
            return new Result1(0,"添加成功");
        }
        return new Result1(1,"添加失败");
    }

    @RequestMapping("/updateMessage")
    public Object updateMessage(Integer id) {
        Message message=new Message();
        message.setId(id);
        message.setIsOpen("已打开");
        boolean update = iMessageService.update(message, new QueryWrapper<Message>().eq("id", id));
        if(update) {
            return new Result(0,"成功");
        }
        return new Result(1,"失败");
    }


    @RequestMapping("/updateBathMessage")
    public Object updateBathMessage(Integer uid) {
        Message message=new Message();
        message.setIsOpen("已打开");
        message.setAcceptId(uid);
        boolean update2 = iMessageService.update(message,new QueryWrapper<Message>().eq("accept_id", uid));
        if(update2) {
            return new Result(0,"成功");
        }
        return new Result(1,"失败");
    }
}
